package com.sd.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sd.dao.Dict;
import com.sd.util.MultipleTree;

public class DictController extends Controller {
	public void getList(){
		String sql = "select * from dict";
		renderJson(Dict.dao.find(sql));
	}
	public void getTree(){
		//int p_id = getParaToInt("p_id")==null?0:getParaToInt("p_id");
		String sql = "select * from dict";
		List<Record> dataList = Db.find(sql);
		String jsonStr = MultipleTree.getTree(dataList);
		renderJson(jsonStr);
	}
	public void save(){
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean flag = false;
		try{
			flag = getModel(Dict.class).save();
		}catch (Exception e) {
			e.printStackTrace();
		}
		if(flag){
			map.put("result", "true");
			map.put("message", "保存成功");
		}else{
			map.put("result", "false");
			map.put("message", "保存失败");
		}
		renderJson(map);
	}
	
	public void delete(){
		Map<String, Object> map = new HashMap<String, Object>();
		int flag = 0;
		int id = getParaToInt("dict.dict_id");
		try{
			flag = Db.update("delete from dict where dict_id = ?",id);
		}catch (Exception e) {
			e.printStackTrace();
		}
		if(flag>0){
			map.put("result", "true");
			map.put("message", "删除成功");
		}else{
			map.put("result", "false");
			map.put("message", "删除失败");
		}
		renderJson(map);
	}
	
	public void update(){
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean flag = false;
		try{
			flag = getModel(Dict.class).update();
		}catch (Exception e) {
			e.printStackTrace();
		}
		if(flag){
			map.put("result", "true");
			map.put("message", "保存成功");
		}else{
			map.put("result", "false");
			map.put("message", "保存失败");
		}
		renderJson(map);
	}
}



